﻿ package swing;

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import org.apache.ibatis.session.SqlSession;

import util.DataBaseCreate;
import util.GetSqlSession;
import util.MD5Util;
import util.SelfStarting;
import baseDao.RangeLoginDao;
import baseDao.UserDao;
import baseDao.VpnDao;
import entity.User;
import entity.Vpn;

public class Login extends JFrame {
	public static User us =null;
	
	public static Vpn vp =null;
	public static Login login = new Login();
	private static String username="admin";
	
	// 账号
	static JTextField jt1;

	public Login() {
		super("Login");
		this.setDefaultCloseOperation(3);
		this.setSize(420, 320);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		String imgPath = "image/01.png";  
        try {
            BufferedImage image = ImageIO.read(new FileInputStream(imgPath));
            this.setIconImage(image);
            
        }  catch (IOException e) {
             
        }


		Container c = this.getContentPane();
		c.setBackground(new Color(148, 196, 255));
		c.setLayout(null);

		/*
		 * c.addMouseMotionListener(new MouseMotionListener() {
		 * 
		 * @Override public void mouseMoved(MouseEvent e) { int x=e.getX(); int
		 * y=e.getY(); System.out.println(x+"  "+y); }
		 * 
		 * @Override public void mouseDragged(MouseEvent e) { } });
		 */

		// 背景图片
		JLabel j = new JLabel(new ImageIcon("image/loginBg.jpg"));
		j.setBounds(0, 0, 380, 150);
		j.setBackground(Color.blue);
		//j.setVerticalAlignment(SwingUtilities.CENTER);
		c.add(j);

		// 头像
		// JLabel j2 = new JLabel(new ImageIcon("image/touxiang.png"));
		// j2.setBounds(35, 165, 75, 75);
		// c.add(j2);
		JLabel j2 = new JLabel("账号：");
		j2.setBounds(60, 165, 60, 30);
		c.add(j2);
		JLabel jl3 = new JLabel("密码：");
		jl3.setBounds(60, 198, 60, 30);
		c.add(jl3);

		// 账号框
		jt1 = new JTextField();
		jt1.setBounds(115, 165, 180, 30);
		c.add(jt1);

		// 注册账号
		// JButton jb1 = new JButton("注册账号");
		// jb1.setBounds(300, 172, 100, 20);
		// jb1.setBackground(Color.orange);
		// c.add(jb1);

		// 密码框
		final JPasswordField jw = new JPasswordField();
		jw.setBounds(115, 195, 180, 30);
		c.add(jw);

		// 修改密码
		JButton jb2 = new JButton("vpn设置");
		jb2.setBounds(300, 203, 80, 20);
		jb2.setBackground(Color.orange);
		jb2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				UpdatePassword text = new UpdatePassword(Login.this);
				text.setVisible(true);
			}
		});

		c.add(jb2);

		// 记住密码复选框
		/*
		 * JCheckBox jc = new JCheckBox("记住密码"); jc.setBounds(115, 230, 100,
		 * 15); c.add(jc);
		 */

		// 自动登录复选框
		/*
		 * JCheckBox jc1 = new JCheckBox("自动登录"); jc1.setBounds(210, 230, 100,
		 * 15); c.add(jc1);
		 */

		// 登录
		JButton j3 = new JButton();
		ImageIcon icon = new ImageIcon("image/login.png");
		j3.setIcon(icon);
		j3.setBounds(115, 250, 180, 30);

		j3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				// 获取用户输入的账号
				username = jt1.getText();

				// 获取用户输入的密码
				char[] c = jw.getPassword();
				String password = new String(c);

				// 根据用户输入的密码，加密，
				//password = MD5Util.convertMD5(password);

				System.out.println("输入登录密码:" + password);
				// user构造器
				User u = new User(null, username, password, null, null,null, null,"true");
				// 登录
				u= RangeLoginDao.login(u);
				us=u;
				System.out.println(us);
				if (u.getId() == null ) {
					JOptionPane.showMessageDialog(null, "您输入的账号或者密码不正确...");
				} else if(u.getExpireTime()<System.currentTimeMillis()){
					JOptionPane.showMessageDialog(null, "您的账号已过期,请及时续费!!!");
				}else{
					// 登录后，修改flag=1
					SqlSession session = GetSqlSession.getSession();
					UserDao userDao = session.getMapper(UserDao.class);
					u.setUserpassword(us.getId().toString());
					userDao.updateUser(u);
					User user = userDao.selectUserByUsername(us.getUsername());
					user.setFalg(1);
					int flag = userDao.updateUserFlag(user);
					System.out.println(flag);
					login.setVisible(false);
					// 进入主页面
					new Index();
					GetSqlSession.closeSession(session);
				}
			}
		});
		c.add(j3);
	}

	// 根据用户名获取id
	public static User getUser() {

//		SqlSession session = GetSqlSession.getSession();
//		UserDao userDao = session.getMapper(UserDao.class);
//		User user = userDao.selectById();
		// System.out.println(user);
		return us;
	}
	
	public static Vpn getVP(){
		if(vp==null){
			SqlSession session = GetSqlSession.getSession();
			// 获取user接口:
			VpnDao vpDao = session.getMapper(VpnDao.class);
			vp=vpDao.selectAll();
			session.close();
		}
		return vp;
	}
	
	public static void main(String[] args) {
	
		
		// 创建数据库
		DataBaseCreate.create();
		
		SqlSession session = GetSqlSession.getSession();
		// 获取user接口:
		UserDao userDao = session.getMapper(UserDao.class);

		us= userDao.selectById();
		
		Integer falg = us.getFalg();


		/*try {
			SqlSession session1 = GetSqlSession.getSession();
			// 获取user接口:
			UserDao userDao1 = session1.getMapper(UserDao.class);
			User u=new User(null,"admin", null, null,  null,  null, "false",true);
			System.out.println(Boolean.parseBoolean(Login.getUser().getFirst()));
			userDao1.updateUserFirst(u);
		} catch (Exception e) {
			e.printStackTrace();
		}	*/ 	
		System.out.println(us);
		if(!Boolean.parseBoolean(us.isCheck())){
			try {
				SelfStarting.changeStart(false);
				System.out.println(111);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if (falg == 1&&!us.getUsername().equals("1")) {
			// 进入主页面
			us.setId(Integer.parseInt(us.getUserpassword()));
			us=RangeLoginDao.validate(us);
			new Index();
		} else {
			login.setVisible(true);
		}
		//login.setVisible(true);
	}
}
